
#include "Light.hpp"
#include <Utility/Tools.hpp>

namespace zzz{
Light::Light()
{
  mylight_=0;
  pos_[0]=0;  pos_[1]=0;  pos_[2]=0;  pos_[3]=0;
  amb_[0]=0; amb_[1]=0; amb_[2]=0; amb_[3]=0;
  dif_[0]=0; dif_[1]=0; dif_[2]=0; dif_[3]=0;
  spe_[0]=0; spe_[1]=0; spe_[2]=0; spe_[3]=0;
  inited_=false;
}

void Light::Enable(GLenum l/*=0*/)
{
  if (inited_==false) return;
  if (l==0)
  {
    if (mylight_==0) mylight_=GL_LIGHT0;
  }
  else
    mylight_=l;
  glEnable(GL_LIGHTING);
  glEnable(mylight_);
  glLightfv(mylight_,GL_POSITION,pos_);
  glLightfv(mylight_,GL_AMBIENT,amb_);
  glLightfv(mylight_,GL_DIFFUSE,dif_);
  glLightfv(mylight_,GL_SPECULAR,spe_);
}

void Light::Disable()
{
  if (inited_==false) return;
  if (mylight_==0) return;
  glDisable(mylight_);
}

void Light::Disable(GLenum l)
{
  glDisable(l);
}

void Light::DisableAll()
{
  glDisable(GL_LIGHTING);
}

void Light::DrawLine()
{
  glBegin(GL_LINES);
  glColor4fv(dif_);
  glVertex3f(0,0,0);
  glVertex4fv(pos_);
  glEnd();
}

void Light::Rotate(const GLfloat *rotmat)
{
  pos_[0]=rotmat[0]*oripos_[0]+rotmat[4]*oripos_[1]+rotmat[8]*oripos_[2]+rotmat[12]*oripos_[3];
  pos_[1]=rotmat[1]*oripos_[0]+rotmat[5]*oripos_[1]+rotmat[9]*oripos_[2]+rotmat[13]*oripos_[3];
  pos_[2]=rotmat[2]*oripos_[0]+rotmat[6]*oripos_[1]+rotmat[10]*oripos_[2]+rotmat[14]*oripos_[3];
  pos_[3]=rotmat[3]*oripos_[0]+rotmat[7]*oripos_[1]+rotmat[11]*oripos_[2]+rotmat[15]*oripos_[3];
  glLightfv(mylight_,GL_POSITION,pos_);
}
}